package com.grapevine.contact;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.text.method.DialerKeyListener;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;

public class DialActivity extends Activity implements OnClickListener,
		TextWatcher {
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.twelve_key_dialer);
		mDigits = (EditText) findViewById(R.id.digits);
		findViewById(R.id.one).setOnClickListener(this);
		findViewById(R.id.two).setOnClickListener(this);
		findViewById(R.id.three).setOnClickListener(this);
		findViewById(R.id.four).setOnClickListener(this);
		findViewById(R.id.five).setOnClickListener(this);
		findViewById(R.id.six).setOnClickListener(this);
		findViewById(R.id.seven).setOnClickListener(this);
		findViewById(R.id.eight).setOnClickListener(this);
		findViewById(R.id.nine).setOnClickListener(this);
		findViewById(R.id.zero).setOnClickListener(this);
		findViewById(R.id.deleteButton).setOnClickListener(this);
		findViewById(R.id.pound).setOnClickListener(this);
		findViewById(R.id.star).setOnClickListener(this);
		findViewById(R.id.voicemailButton).setOnClickListener(this);
		findViewById(R.id.dialButton).setOnClickListener(this);

//		mDigits.setLayoutParams(new LinearLayout.LayoutParams(
//				LinearLayout.LayoutParams.FILL_PARENT, 60));
		
//		
//		mDigits.setLayoutParams(new LinearLayout.LayoutParams(
//				LinearLayout.LayoutParams.FILL_PARENT, 60));
		
//		mDigits.invalidate(0, 0, 100, 5);
		
//		mDigits.invalidate();
		
//		mDigits.layout(0, 0, 100, 10);
		
//		mDigits.refreshDrawableState();
		
		
//		mDigits.

		mDigits.setOnClickListener(this);
		mDigits.addTextChangedListener(this);
		mDigits.setKeyListener(DialerKeyListener.getInstance());
		mDigits.setInputType(android.text.InputType.TYPE_NULL);
		findViewById(R.id.deleteButton).setOnLongClickListener(
				new OnLongClickListener() {

					@Override
					public boolean onLongClick(View v) {
						mDigits.setText("");
						return true;
					}
				});

	}

	@Override
	public void startSearch(String initialQuery, boolean selectInitialQuery,
			Bundle appSearchData, boolean globalSearch) {
		super.startSearch(initialQuery, selectInitialQuery, appSearchData,
				globalSearch);
	}

	private EditText mDigits;

	@Override
	public void onWindowFocusChanged(boolean hasFocus) {
		if (hasFocus) {
			InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
			inputMethodManager.hideSoftInputFromWindow(
					mDigits.getWindowToken(), 0);
		}
	}

	@Override
	protected void onResume() {
		super.onResume();
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.one:
			keyPressed(KeyEvent.KEYCODE_1);
			break;

		case R.id.two:
			keyPressed(KeyEvent.KEYCODE_2);
			break;

		case R.id.three:
			keyPressed(KeyEvent.KEYCODE_3);
			break;

		case R.id.four:
			keyPressed(KeyEvent.KEYCODE_4);
			break;

		case R.id.five:
			keyPressed(KeyEvent.KEYCODE_5);
			break;

		case R.id.six:
			keyPressed(KeyEvent.KEYCODE_6);
			break;

		case R.id.seven:
			keyPressed(KeyEvent.KEYCODE_7);
			break;

		case R.id.eight:
			keyPressed(KeyEvent.KEYCODE_8);
			break;

		case R.id.nine:
			keyPressed(KeyEvent.KEYCODE_9);
			break;

		case R.id.zero:
			keyPressed(KeyEvent.KEYCODE_0);
			break;

		case R.id.star:
			keyPressed(KeyEvent.KEYCODE_STAR);
			break;

		case R.id.pound:
			keyPressed(KeyEvent.KEYCODE_POUND);
			break;
		case R.id.deleteButton:
			KeyEvent event = new KeyEvent(KeyEvent.ACTION_DOWN,
					KeyEvent.KEYCODE_DEL);
			mDigits.onKeyDown(KeyEvent.KEYCODE_DEL, event);
			break;

		case R.id.digits:
			if (!TextUtils.isEmpty(mDigits.getText().toString())) {
				mDigits.setCursorVisible(true);
			}
			break;
		case R.id.dialButton:
			callNumber();
			break;
		case R.id.voicemailButton:
			callSmsMail();
			break;
		default:
			break;
		}
	}

	private void keyPressed(int keyCode) {
		KeyEvent event = new KeyEvent(KeyEvent.ACTION_DOWN, keyCode);
		mDigits.onKeyDown(KeyEvent.KEYCODE_DEL, event);
	}

	void callNumber() {
		String number = mDigits.getText().toString();
		if (!TextUtils.isEmpty(number)) {
			Intent it = new Intent(Intent.ACTION_CALL, Uri.parse("tel: "
					+ number));
			startActivity(it);
			finish();
		}
	}

	void callSmsMail() {
		String number = mDigits.getText().toString();
		if (!TextUtils.isEmpty(number)) {
			Intent mmsintent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts(
					"smsto", number, null));
			startActivity(mmsintent);
		}
	}

	@Override
	public void afterTextChanged(Editable s) {
		if (TextUtils.isEmpty(mDigits.getText().toString())) {
			mDigits.setCursorVisible(false);
		}
	}

	@Override
	public void beforeTextChanged(CharSequence s, int start, int count,
			int after) {
	}

	@Override
	public void onTextChanged(CharSequence s, int start, int before, int count) {
	}
}